package cn.rui.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.rui.aspects.Log;
import cn.rui.common.Result;
import cn.rui.pojo.User;
import cn.rui.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 徽州大都督
 * @date 2021/05/13
 */
@Controller
@RequestMapping("/user/")
public class UserController {

    @Autowired
    private UserService userService;

    // 测试登录，浏览器访问： http://localhost:8085/user/doLogin?username=zhang&password=123456
    @RequestMapping("doLogin")
    @ResponseBody
    public Result doLogin(@RequestBody User user) {
        return userService.login (user);
    }

    // 查询登录状态，浏览器访问： http://localhost:8085/user/isLogin
    @RequestMapping("isLogin")
    @ResponseBody
    public String isLogin(String username, String password) {
        return "当前会话是否登录：" + StpUtil.isLogin ();
    }

    /**
     * 注销
     *
     * @return
     */
    @RequestMapping("logout")
    @ResponseBody
    public Result logout() {
        StpUtil.logout ();
        return Result.getSuccess ("注销成功!");
    }


    /**
     * 查询所有用户
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("findAll")
    public Result findAll() {
        return userService.findAll ();
    }

    /**
     * 踢人下线
     *
     * @param uuid
     * @return
     */
    @ResponseBody
    @RequestMapping("logoutByLoginId/{uuid}")
    public Result logoutByLoginId(@PathVariable("uuid") String uuid) {
        return userService.logoutByLoginId (uuid);
    }


    /**
     * 账号封禁
     *
     * @param uuid
     * @param statu 0 封禁  1解封
     * @return
     */
    @ResponseBody
    @RequestMapping("disable/{uuid}/{statu}")
    public Result disable(@PathVariable("uuid") String uuid, @PathVariable("statu") Integer statu) {
        return userService.disable (uuid, statu);
    }


    /**
     * 修改密码
     *
     * @param oldPass
     * @param checkPass
     * @return
     */
    @Log("修改密码")
    @ResponseBody
    @RequestMapping("updatePass/{oldPass}/{checkPass}/{loginName}")
    public Result updatePass(@PathVariable("oldPass") String oldPass, @PathVariable("checkPass") String checkPass, @PathVariable("loginName") String loginName) {
        return userService.updatePass (oldPass, checkPass, loginName);
    }

    @ResponseBody
    @PostMapping("add/{addName}/{checkedPermissions}")
    @Log("管理员添加用户")
    public Result add(@PathVariable("addName") String addName, @PathVariable("checkedPermissions") List<Integer> permissionIds) {
        return userService.add (addName, permissionIds);

    }
}
